package com.bokee.archerwq.lab.timer;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

public class TestTimerThread {
	protected static Logger logger = Logger.getLogger(TestTimerThread.class);

	public static void main(String[] args) {
		logger.info("in main...");
		TestTimerThread ttt = new TestTimerThread();
		Timer timer = new Timer();
		TimerTask task = ttt.new MyTimerTask(timer);
		timer.scheduleAtFixedRate(task, 100, 5000);
		timer.scheduleAtFixedRate(task, 100, 5000);
		logger.info("out main...");
	}

	class MyTimerTask extends TimerTask {
		private Timer timer;

		public MyTimerTask(Timer timer) {
			this.timer = timer;
		}

		@Override
		public void run() {
			logger.info("in run...");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			this.cancel();
			timer.cancel();
			logger.info("out run...");
		}
	}

}
